package com.pearadmin.modules.sys.controller.manage;

import com.github.pagehelper.PageInfo;
import com.pearadmin.common.constant.ControllerConstant;
import com.pearadmin.common.tools.SecurityUtil;
import com.pearadmin.common.tools.SequenceUtil;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import com.pearadmin.modules.sys.domain.CarInfo;
import com.pearadmin.modules.sys.domain.SysUser;
import com.pearadmin.modules.sys.service.ICarInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;

/**
 * 车辆信息管理Controller
 *
 * @author Mr.Ling
 * @date 2021-11-26
 */
@RestController
@Api(tags = {"车辆信息管理"})
@RequestMapping(ControllerConstant.API_MANAGE_PREFIX + "carInfo")
public class CarInfoController extends BaseController {
    private String prefix = "manage/carInfo" ;

    @Autowired
    private ICarInfoService carInfoService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/manage/carInfo/main','manage:carInfo:main')")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询车辆信息管理列表
     */
    @ResponseBody
    @ApiOperation("列表")
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/manage/carInfo/data','manage:carInfo:data')")
    public ResultTable list(@ModelAttribute CarInfo carInfo, PageDomain pageDomain) {
        PageInfo<CarInfo> pageInfo = carInfoService.selectCarInfoPage(carInfo, pageDomain);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 新增车辆信息管理
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/manage/carInfo/add','manage:carInfo:add')")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存车辆信息管理
     */
    @ResponseBody
    @ApiOperation("新增")
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/manage/carInfo/add','manage:carInfo:add')")
    public Result save(@RequestBody CarInfo carInfo) {
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        carInfo.setCreateTime(LocalDateTime.now());
        carInfo.setCreateBy(sysUser.getUserId());
        carInfo.setCreateName(sysUser.getRealName());
        carInfo.setId(SequenceUtil.makeStringId());
        return decide(carInfoService.insertCarInfo(carInfo));
    }

    /**
     * 修改车辆信息管理
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/manage/carInfo/edit','manage:carInfo:edit')")
    public ModelAndView edit(String id, ModelMap mmap) {
        CarInfo carInfo = carInfoService.selectCarInfoById(id);
        carInfo.setUpdateTime(LocalDateTime.now());
        mmap.put("carInfo", carInfo);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存车辆信息管理
     */
    @ResponseBody
    @ApiOperation("更新")
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/manage/carInfo/edit','manage:carInfo:edit')")
    public Result update(@RequestBody CarInfo carInfo) {
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        carInfo.setUpdateTime(LocalDateTime.now());
        carInfo.setUpdateBy(sysUser.getUserId());
        carInfo.setUpdateName(sysUser.getUsername());
        return decide(carInfoService.updateCarInfo(carInfo));
    }

    /**
     * 删除车辆信息管理
     */
    @ResponseBody
    @ApiOperation("批量刪除")
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/manage/carInfo/remove','manage:carInfo:remove')")
    public Result batchRemove(String ids) {
        return decide(carInfoService.deleteCarInfoByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @ApiOperation("根据id 刪除")
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/manage/carInfo/remove','manage:carInfo:remove')")
    public Result remove(@PathVariable("id") String id) {
        return decide(carInfoService.deleteCarInfoById(id));
    }
}
